<template>
  <article class="page">
    <headerbar>发布</headerbar>
    <section class="frame-content-sub no-bg">

      <section class="inner-content pdv-9 pdh-4">
        <textarea class="line-input" placeholder="请输入您要发布的内容" id="" cols="30" rows="3" v-model="content"></textarea>
        <div class="line"></div>
        <div class="mgt-10"></div>
        <section class=" upload-wrap">
        <div class="input-row-wrap">
          <div class="mgt-5 color-tip mgb-5">添加附件</div>
        </div>
          <cube-upload ref="upload"
                       action=""
                       v-model="files"
                       accept="mutiimagevideo/*"
                       :simultaneous-uploads="1"
                       :multiple="!0"
                       @files-added="uploadFile"
                       @file-removed="fileRemoved"
                       @input="onInput"
          />
          <div class="m-text" v-if="false">添加照片</div>
        </section>


        <div class="pdv-10"></div>
        <div class="pdv-10"></div>

        <div class="default-block-btn" v-preventReClick="1000" @click="submit($event)">立即发布</div>

        <div class="pdv-5"></div>
      </section>

    </section>
  </article>
</template>

<script>
  import footerbar from '../../../components/layout/footerbar.vue'
  import tools from '@/utils/tools'
  import api from '@/common/config/api'
  import loginService from '@/service/loginService'
  import moment from 'moment'
  import fileIcon from '../../../../static/images/up_document.png'
  export default {
    name: 'redfriendsRelease',
    data () {
      return {
        content: '',
        files: [],
        attachmentList: [],
        from: '',
        uploadStatus: 1, //上传状态，0上传中、1全部上传完成
      }
    },
    created () {
      this.from = this.$route.params.from
    },
    methods: {
      submit (e) {
        if (e.target.disabled) {
          return false
        }
        if (this.content === '') {
          return tools.tip('请输入需要发布的内容！')
        }
        if (this.uploadStatus !== 1) { //文件未上传完
          return tools.tip('文件上传中，请稍候···')
        }
        let files = ''
        let filesOssUrl = ''
        let index = -1
        this.attachmentList.map((v, i) => {
          files = files + v.fileid + ','
          filesOssUrl = filesOssUrl + v.filesOssUrl + ','
          index = i
        })
        if (index !== -1) {
          files = files.substring(0, files.length - 1)
          filesOssUrl = filesOssUrl.substring(0, filesOssUrl.length - 1)
        }
        console.log("------------files=", files)
        let data = {
          content: this.content,
          publicWay: '2',
          files: files,
          filesOssUrl: filesOssUrl
        }
        if (this.from === 'home') {
          this.releaseNews(data)
        } else {
          this.shareFriendAdd(data)
        }
      },
      // 删除文件
      fileRemoved (file) {
        this.attachmentList.some((v, i) => {
          if (v.fileName === file.file.name && v.totalBytes === file.file.size) {
            this.attachmentList.splice(i, 1)
            return false
          }
        })
      },
      uploadFile (file) {
        console.log('--------------file.length=' + file.length)
        this.uploadStatus = 0;//进入for循环以前就置为不能提交
        Array.prototype.slice.call(file).forEach((v) => {
          let formData = new FormData()
          formData.append('file', v)
          loginService.o_upload(formData).then((res) => {
            if (res.code !== 0) {
              return tools.tip(res.message)
            }
            tools.tip('上传成功！')
            this.attachmentList.push({'fileid': res.data.fileId,'filesOssUrl':res.data.desc,'fileName': v.name, 'totalBytes': res.data.totalBytes, 'type': '1'})
          })
        })
        this.uploadStatus = 1;//for循环结束以后允许提交
      },
      onInput (file) {
        this.$refs.upload.files.map((v) => {
          if (!tools.isImage(v.name)) {
            v.url = fileIcon
          }
          return v
        })
      },
      releaseNews (data) { // 红友圈发布动态
        loginService.releaseNews(data).then((res) => {
          if (res.code !== 0) {
            return tools.tip(res.message)
          } else {
            tools.tip('发布成功')
              return this.$router.replace({path: `/redfriendscircle/home?refresh`});
          }
        })
      },
      shareFriendAdd (data) { // 互动分享发布动态
        loginService.shareFriendAdd(data).then((res) => {
          if (res.code !== 0) {
            return tools.tip(res.message)
          } else {
            tools.tip(res.message)
            return this.$router.replace({path: `/redfriendscircle/home?refresh`});
          }
        })
      }
    }
  }
</script>

<style scoped lang="scss">
.line-input {
  font-size:16PX;
  font-family: dang;
}
textarea::-webkit-input-placeholder{
  /*color:red;*/
  line-height: 1.5em;
  font-size:16PX;
  font-family: dang,PingFang SC,miui,system-ui,-apple-system,BlinkMacSystemFont,Helvetica Neue,Helvetica,sans-serif;

}
</style>
